package com.jianbing.queue;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.ons.model.v20160503.OnsConsumerStatusRequest;
import com.aliyuncs.ons.model.v20160503.OnsConsumerStatusResponse;
import com.aliyuncs.ons.model.v20160503.OnsSubscriptionGetRequest;
import com.aliyuncs.ons.model.v20160503.OnsSubscriptionGetResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.jianbing.util.ColletionUtils;
import com.jianbing.util.PropertiesUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;

/**
 * Created by Han, Tixiang on 2016/11/18 0018.
 */
public class RocketMQManager {
    private static Log log = LogFactory.getLog(RocketMQManager.class);

    public static OnsSubscriptionGetResponse.SubscribeInfoDo OnsSubscribeGet(String topic, String consumerId) {
        String regionId = PropertiesUtils.getProperties("regionId");
        String accessKey = PropertiesUtils.getProperties("AccessKey");
        String secretKey = PropertiesUtils.getProperties("SecretKey");
        String endPointName = PropertiesUtils.getProperties("regionId");
        String productName = PropertiesUtils.getProperties("productName");
        String domain = PropertiesUtils.getProperties("domain");
        /**
         *根据自己需要访问的区域选择Region,并设置对应的接入点
         */
        try {
            DefaultProfile.addEndpoint(endPointName, regionId, productName, domain);
        } catch (ClientException e) {
            log.error(e.toString(), e);
        }
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKey, secretKey);
        IAcsClient iAcsClient = new DefaultAcsClient(profile);
        OnsSubscriptionGetRequest request = new OnsSubscriptionGetRequest();
        /**
         *ONSRegionId是指你需要API访问MQ哪个区域的资源
         *该值必须要根据OnsRegionList方法获取的列表来选择和配置,因为OnsRegionId是变动的,不能够写固定值
         */
        request.setOnsRegionId(regionId);
        request.setPreventCache(System.currentTimeMillis());
        request.setAcceptFormat(FormatType.JSON);
        request.setTopic(topic);
        request.setConsumerId(consumerId);
        try {
            OnsSubscriptionGetResponse response = iAcsClient.getAcsResponse(request);
            List<OnsSubscriptionGetResponse.SubscribeInfoDo> subscribeInfoDoList = response.getData();
            if (ColletionUtils.isEmpty(subscribeInfoDoList)) {
                return null;
            }
            for (OnsSubscriptionGetResponse.SubscribeInfoDo subscribeInfoDo : subscribeInfoDoList) {
              /*  log.info(subscribeInfoDo.getId() + "  " +
                        subscribeInfoDo.getChannelId() + "  " +
                        subscribeInfoDo.getChannelName() + "  " +
                        subscribeInfoDo.getOnsRegionId() + "  " +
                        subscribeInfoDo.getRegionName() + "  " +
                        subscribeInfoDo.getOwner() + "  " +
                        subscribeInfoDo.getConsumerId() + "  " +
                        subscribeInfoDo.getTopic() + "  " +
                        subscribeInfoDo.getStatus() + "  " +
                        subscribeInfoDo.getStatusName() + " " +
                        subscribeInfoDo.getCreateTime() + "  " +
                        subscribeInfoDo.getUpdateTime());*/

                if (subscribeInfoDo.getConsumerId().equals(consumerId)) {
                    return subscribeInfoDo;
                }

            }
        } catch (ServerException e) {
            log.error(e.toString(), e);
        } catch (ClientException e) {
            log.error(e.toString(), e);
        }
        return null;
    }


    public static OnsConsumerStatusResponse.Data onsConsumerStatus(String consumerId) {

        String regionId = PropertiesUtils.getProperties("regionId");
        String accessKey = PropertiesUtils.getProperties("AccessKey");
        String secretKey = PropertiesUtils.getProperties("SecretKey");
        String endPointName = PropertiesUtils.getProperties("regionId");
        String productName = PropertiesUtils.getProperties("productName");
        String domain = PropertiesUtils.getProperties("domain");
        /**
         *根据自己需要访问的区域选择Region,并设置对应的接入点
         */
        try {
            DefaultProfile.addEndpoint(endPointName, regionId, productName, domain);
        } catch (ClientException e) {
            log.error(e.toString(), e);
        }
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKey, secretKey);
        IAcsClient iAcsClient = new DefaultAcsClient(profile);
        OnsConsumerStatusRequest request = new OnsConsumerStatusRequest();
        /**
         *ONSRegionId是指你需要API访问MQ哪个区域的资源.
         *该值必须要根据OnsRegionList方法获取的列表来选择和配置,因为OnsRegionId是变动的,不能够写固定值
         */
        request.setOnsRegionId(regionId);
        request.setPreventCache(System.currentTimeMillis());
        request.setAcceptFormat(FormatType.JSON);
        request.setConsumerId(consumerId);
        request.setDetail(false);
        request.setNeedJstack(false);
        try {
            OnsConsumerStatusResponse response = iAcsClient.getAcsResponse(request);
            OnsConsumerStatusResponse.Data data = response.getData();
            /*List<OnsConsumerStatusResponse.Data.ConnectionDo> connectionDoList = data.getConnectionSet();
            List<OnsConsumerStatusResponse.Data.DetailInTopicDo> detailInTopicDoList = data.getDetailInTopicList();
            List<OnsConsumerStatusResponse.Data.ConsumerConnectionInfoDo> consumerConnectionInfoDoList = data.getConsumerConnectionInfoList();*/
           /* String s = "" + (data.getOnline() + "  " + data.getTotalDiff() + "  " + data.getConsumeTps() + "  " +
                    data.getLastTimestamp() + "  " + data.getDelayTime() + "  " + data.getConsumeModel() +
                    "  " + data.getSubscriptionSame() + "  " + data.getRebalanceOK());*/
           // log.info(s);
       /*     list.addAll(connectionDoList);
            list.add(detailInTopicDoList);
            list.add(consumerConnectionInfoDoList);*/
            return data;
        } catch (ServerException e) {
            log.error(e.toString(), e);
        } catch (ClientException e) {
            log.error(e.toString(), e);
        }
        return null;
    }

    public static void main(String[] args) {
        Object object1 = onsConsumerStatus("CID_RECEIVER_JIANBING_TEST_GJJ_YZM");
        Object object = OnsSubscribeGet("jianbing_shebao_topic", "CID_RECEIVER_GJJ_YZM_ID_JIANBING");
    }
}
